package com.it.myh;

import java.util.Arrays;
import java.util.Objects;

/**
 * @Description: // 类说明，在创建类时要填写
 * @ClassName: Ev_0206_02    // 类名，会自动填充
 * @Author: MYH          // 创建者
 * @Date: 2023/2/6 9:39   // 时间
 * @Version: 1.0     // 版本
 */
public class Ev_0206_02 {

    public static void main(String[] args) {
        Goods[] arr = new Goods[5];
        arr[0] = new Goods("联想",40);
        arr[1] = new Goods("黑麒麟",45);
        arr[2] = new Goods("牧马人",12);
        arr[3] = new Goods("三星",4);
        arr[4] = new Goods("test",2);
        try {
            Arrays.sort(arr);
        } catch (RuntimeException e) {
            System.out.println(e.getMessage());
        }
        for (Object o : arr) {
            System.out.println(o);
        }

        int i = arr[0].compareTo(arr[1]);
    }


}
class Goods implements Comparable{

    private String name;

    private double price;

    public Goods() {
    }

    public Goods(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Goods goods = (Goods) o;
        return Double.compare(goods.price, price) == 0 && Objects.equals(name, goods.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, price);
    }

    //指明商品比较大小的方式
    @Override
    public int compareTo(Object o) {
        if (o instanceof Goods){
            Goods goods = (Goods) o;
            if (this.price > goods.price){
                return -1;
            }else if (this.price < goods.price){
                return 1;
            }else {
                return 0;
            }
        }
        throw new RuntimeException("传入的数据类型不一致");
    }
}
